package com.caiyi.financial.nirvana.sms.cmpp.message.body;

import com.caiyi.financial.nirvana.sms.cmpp.message.CMPP3Body;
import io.netty.buffer.ByteBuf;

import java.util.HashMap;
import java.util.Map;

public class CMPP3ConnectionResp extends CMPP3Body {

    public final static Map<Integer, String> STATUS = new HashMap<>();

    //登录状态
    private int status;

    //网关验证值
    private String authenticatorISMG;

    //connection消息提交的验证值
    private String authenticator;

    //最大版本号
    private byte maxVersion;

    static {
        STATUS.put(0, "正确");
        STATUS.put(1, "消息结构错");
        STATUS.put(2, "非法源地址");
        STATUS.put(3, "认证错");
        STATUS.put(4, "版本太高");
        STATUS.put(5, "其他错误");
    }

    @Override
    public int encode(ByteBuf buffer) {
        int length = 0;
        buffer.writeInt(status);
        length += 4;

        buffer.writeZero(16);
        length += 16;

        buffer.writeByte(0x30);
        length += 1;

        return length;
    }

    @Override
    public CMPP3Body decode(ByteBuf buffer) {
        status = buffer.readInt();
        byte[] auth = new byte[16];
        buffer.readBytes(auth);
        authenticatorISMG = new String(auth);
        maxVersion = buffer.readByte();
        return this;
    }


    /**
     * @return 登录状态
     */
    public int getStatus() {
        return status;
    }

    /**
     * @return 网关验证值
     */
    public String getAuthenticatorISMG() {
        return authenticatorISMG;
    }

    /**
     * @return 最大版本号
     */
    public byte getMaxVersion() {
        return maxVersion;
    }

    public void setAuthenticator(String authenticator) {
        this.authenticator = authenticator;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getAuthenticator() {
        return authenticator;
    }
}
